//=============================================================================
// d3dUtil.h
//
// Contains various utility code for DirectX applications, such as, clean up
// and debugging code.
//=============================================================================
#pragma once

// Enable extra D3D debugging in debug builds if using the debug DirectX runtime.  
// This makes D3D objects work well in the debugger watch window, but slows down 
// performance slightly.
#if defined(DEBUG) | defined(_DEBUG)
#ifndef D3D_DEBUG_INFO
#define D3D_DEBUG_INFO
#endif
#endif

#include <d3d9.h>
#include <d3dx9.h>
#include <d3dx9math.h>
#include <dxerr.h>
#include <string>
#include <sstream>
#include <vector>

#pragma comment (lib, "DxErr.lib")
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")

//===============================================================
// Globals for convenient access.
class CDirectXFramework;
extern CDirectXFramework* gd3dApp;
extern IDirect3DDevice9* gd3dDevice;

#include "DirectInput.h"
extern DirectInput* gDInput;
//===============================================================
// Clean up

#define ReleaseCOM(x) { if (x){ x->Release();x = 0; } }

//===============================================================
// Geometry generation.

void GenTriGrid(int numVertRows,
				int numVertCols, 
				float dx,
				float dz,
				const D3DXVECTOR3& center, 
				std::vector<D3DXVECTOR3>& verts,
				std::vector<DWORD>& indices);

//===============================================================
// Colors and Materials

const D3DXCOLOR WHITE(1.0f, 1.0f, 1.0f, 1.0f);
const D3DXCOLOR BLACK(0.0f, 0.0f, 0.0f, 1.0f);
const D3DXCOLOR RED(1.0f, 0.0f, 0.0f, 1.0f);
const D3DXCOLOR GREEN(0.0f, 1.0f, 0.0f, 1.0f);
const D3DXCOLOR BLUE(0.0f, 0.0f, 1.0f, 1.0f);

struct Mtrl
{
	Mtrl()
		:ambient(WHITE), diffuse(WHITE), specular(WHITE), specularPower(8.0f)
	{}
	Mtrl(const D3DXCOLOR& a, const D3DXCOLOR& d, const D3DXCOLOR& s, float power)
		:ambient(a), diffuse(d), specular(s), specularPower(power)
	{}

	D3DXCOLOR ambient;
	D3DXCOLOR diffuse;
	D3DXCOLOR specular;
	float specularPower;
};

//===============================================================
// Debug

#if defined(DEBUG) | defined(_DEBUG)
	#ifndef HR
	#define HR(x)                                      \
	{                                                  \
		HRESULT hr = x;                                \
		if (FAILED(hr))                                 \
		{                                              \
			DXTrace(__FILE__, __LINE__, hr, #x, TRUE); \
		}                                              \
	}
	#endif

#else
	#ifndef HR
	#define HR(x) x;
	#endif
#endif 
